package org.jsbd.boss.action;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.domian.statistics.ResidueStatistics;
import org.jsbd.boss.domian.statistics.ResidueStatisticsVo;
import org.jsbd.boss.service.IResidueStatisticsService;
import org.jsbd.boss.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/residueStatistics")
public class ResidueStatisticsController {



  @Autowired
  private IResidueStatisticsService residueStatisticsService;


  /**
   * 新增剩余量、留存剩余量统计页面
   * @return
   */
  	@RequestMapping(value = "/")
  	public String daylimit() {
  		return "residueStatistics/list";
  	}
  
  	/**
  	 * 新增剩余量、留存剩余量统计数据
  	 * @param model
  	 * @param issueStatisticsVo
  	 * @return
  	 */
  	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> list(Model model, ResidueStatisticsVo residueStatisticsVo) {
  		Map<String, Object> map = new HashMap<String, Object>();
  		
  		String sysVer = residueStatisticsVo.getSysVer();
  		String startTime = residueStatisticsVo.getStartTime();
  		String endTime = residueStatisticsVo.getEndTime();
  		if (StringUtils.isBlank(startTime) && StringUtils.isBlank(endTime)) {
  			residueStatisticsVo.setStartTime(DateUtil.getHourBeforeDate(DateUtil.DATE_PATTEN_YY_MM_DD_HHMMSS, 2));
  		}
  		
  		List<String> xAxis =  new ArrayList<String>();
  		List<ResidueStatistics> yAxis =  new ArrayList<ResidueStatistics>();
  		if (StringUtils.isBlank(sysVer)) {
  			yAxis = residueStatisticsService.selectSumRecords(residueStatisticsVo);
  			xAxis = residueStatisticsService.selectSumSysvers(residueStatisticsVo);
  		} else {
  			yAxis = residueStatisticsService.selectRecords(residueStatisticsVo);
  			xAxis = residueStatisticsService.selectMinute(residueStatisticsVo);
  		}
		
		map.put("xAxis", xAxis);
		map.put("yAxis", yAxis);
		return map;
	}
  	
}
